package com.matrix.draft.sockettest.day10.handler;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.nio.charset.StandardCharsets;
import java.util.stream.IntStream;

public class ClientHandler extends SimpleChannelInboundHandler<ByteBuf> {
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        IntStream.range(0, 5).forEach(i -> {
            ctx.writeAndFlush(Unpooled.copiedBuffer("PACKAGE" + i + " ", StandardCharsets.UTF_8));
        });
    }


    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
        byte[] bytes = new byte[msg.readableBytes()];
        msg.readBytes(bytes);
        System.out.println("Got from server : " + new String(bytes, StandardCharsets.UTF_8));
    }
}
